package com.hioove.core.ioc.model;

import com.hioove.core.model.base.config.FieldSupport;

import java.lang.annotation.Annotation;
import java.util.*;

/**
 * 依赖注入的过程使用类
 */
public class AttributeInjection {

    private Map<Class<? extends Annotation>, List<FieldSupport>> toBeInjected;

    // 保存 单例对象
    private Map<Object, Map<Class<?>, Object>> singleton;


    public Map<Class<? extends Annotation>, List<FieldSupport>> getToBeInjected() {
        return Optional.ofNullable(toBeInjected).orElseGet(HashMap::new);
    }

    public void addToBeInjected(Class<? extends Annotation> annotation, FieldSupport fieldSupport) {
        this.toBeInjected = Optional.ofNullable(this.toBeInjected).orElse(new HashMap<Class<? extends Annotation>, List<FieldSupport>>());
        List<FieldSupport> fieldSupports = Optional.ofNullable(this.toBeInjected.get(annotation)).orElse(new ArrayList<>());
        fieldSupports.add(fieldSupport);
        this.toBeInjected.put(annotation, fieldSupports);
    }

    public void setToBeInjected(Map<Class<? extends Annotation>, List<FieldSupport>> toBeInjected) {
        this.toBeInjected = toBeInjected;
    }

    public Map<Object, Map<Class<?>, Object>> getSingleton() {
        return singleton;
    }

    public void addSingleton(Object key, Map<Class<?>, Object> singleton) {
        this.singleton = Optional.ofNullable(this.singleton).orElseGet(HashMap<Object, Map<Class<?>, Object>>::new);
        Map<Class<?>, Object> classObject = Optional.ofNullable(this.singleton.get(key)).orElseGet(HashMap<Class<?>, Object>::new);
        classObject.putAll(singleton);
        this.singleton.put(key, singleton);
    }
}
